package com.lishe.lishe.lishe.module.myorder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.lishe.lishe.R;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.myorder.adapter.OrderModelPagerAdaper;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.module.myorder.fragment.MyOrdersModelFragment;

import java.util.LinkedList;

/**
 * Created by lwa on 2017/8/11.
 * 我的订单viewpager容器
 */

public class MyOrdersActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager viewPager;
    private Button btnBack;
    private TextView texTitle;
    private TabLayout tabLayout;
    private String[] titles;
    private int orderType= Globals.ORDER_TYPE_ALL;

    private OrderModelPagerAdaper pagerAdaper;

    private LinkedList<Fragment> lisOrderFragments = new LinkedList<>();

    public static void jump(Activity activity,int orderType) {
        Intent intent = new Intent(activity, MyOrdersActivity.class);
        intent.putExtra("orderType",orderType);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titles = getResources().getStringArray(R.array.order_type);
        orderType=getIntent().getIntExtra("orderType",Globals.ORDER_TYPE_ALL);
        setContentView(R.layout.activity_my_orders);
        initView();
    }

    private void initView() {
        viewPager = findViewById(R.id.viewpager);
        tabLayout = findViewById(R.id.tabLayout);
        texTitle = findViewById(R.id.title_text_title);
        btnBack = findViewById(R.id.title_left_button);
        setListener();
        initData();
        initFragment();
    }

    private void initData() {
        btnBack.setVisibility(View.VISIBLE);
        texTitle.setVisibility(View.VISIBLE);
        texTitle.setText(R.string.my_order);
    }

    /**
     * 初始化各个状态的订单
     */
    private void initFragment() {
        lisOrderFragments.clear();
        pagerAdaper = new OrderModelPagerAdaper(lisOrderFragments, titles, getSupportFragmentManager());
        for (int i = 0; i < titles.length; i++) {
            lisOrderFragments.add(MyOrdersModelFragment.newInstance(i));
        }
        viewPager.setAdapter(pagerAdaper);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setCurrentItem(orderType);
    }


    private void setListener() {
        btnBack.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            viewPager.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View view, int i, int i1, int i2, int i3) {

                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left_button://返回按钮
                finish();
                break;
        }
    }
}
